#include "KDM2Detector.hh"
#include "KDM2HitsCollection.hh"
#include "KDM2Hit.hh"
#include "G4SDManager.hh"
#include "KDM2Globals.hh"
#include <sstream>
#include <iostream>
using namespace std;

KDM2Detector::KDM2Detector(G4String name) :
	G4VSensitiveDetector(name) {
	hitsCollection = 0;
	collectionName.insert("1");
}

KDM2Detector::~KDM2Detector() {
	;
}

void KDM2Detector::Initialize(G4HCofThisEvent* hcc) {
	hitsCollection = new KDM2HitsCollection(SensitiveDetectorName, collectionName[0]);
	G4SDManager* sdMan = G4SDManager::GetSDMpointer();
	G4int collectionID = sdMan->GetCollectionID(hitsCollection);
	hcc->AddHitsCollection(collectionID, hitsCollection);
}

void KDM2Detector::EndOfEvent(G4HCofThisEvent*) {
	;
}

G4bool KDM2Detector::ProcessHits(G4Step *step, G4TouchableHistory*) {
	double deposit = step->GetTotalEnergyDeposit();
	if (deposit == 0.0)
		return false;

	G4Track* track = step->GetTrack();
	double time = track->GetGlobalTime();
	G4ThreeVector pos = track->GetPosition();
	bool veto = false;

	G4String volName = track->GetVolume()->GetName();
	if (volName.contains("crystalBlock"))
		veto = true;

	KDM2Hit* hit = new KDM2Hit(time, deposit, pos, veto);
	hitsCollection->Insert(hit);

	return true;
}
